package com.warm.test.ws;

import cn.dev33.satoken.annotation.SaIgnore;
import org.noear.solon.net.annotation.ServerEndpoint;
import org.noear.solon.net.websocket.WebSocket;
import org.noear.solon.net.websocket.listener.SimpleWebSocketListener;

import java.io.IOException;

/**
 * 监听模式
 *
 * @author warm
 */
@SaIgnore
@ServerEndpoint("/demo/ws/listener/{id}")
public class WebSocketServerImp extends SimpleWebSocketListener {
    @Override
    public void onOpen(WebSocket socket) {
        //path var
        String id = socket.param("id");
        //query var
        String token = socket.param("token");

        if(("admin".equals(id) && "1234".equals(token)) == false){
            socket.close();
        }

        /*此处可以做签权；会话的二次组织等...*/
    }

    @Override
    public void onMessage(WebSocket socket, String text) throws IOException {
        socket.send("我收到了：" + text);
    }
}
